package javapoker.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.HashMap;

import javapoker.Card;
import javapoker.Hand;
import javapoker.Player;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;

public class TableView extends JFrame {

	private JPanel contentPane;


	
	private JPanel pnlCenter = null;
	private JPanel pnlTop = null;
	private JPanel pnlBot = null;
	private JLabel lblWinner = null;
	private JPanel pnlCommunity = null;
	HashMap<Player, PlayerView> pviews = null;
	
	public JPanel getPanelPlayers()
	{
		return pnlCenter;
	}
	
	public JPanel getPanelTop()
	{
		return pnlTop;
	}
	
	public JPanel getPanelCommunity()
	{
		return pnlCommunity;
	}
	
	/**
	 * Create the frame.
	 */
	public TableView(String title) 
	{
		setTitle(title);
	
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		pnlCenter = new JPanel();
		pnlCenter.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		contentPane.add(pnlCenter, BorderLayout.CENTER);
		
		pnlBot = new JPanel();
		contentPane.add(pnlBot, BorderLayout.SOUTH);
		
		lblWinner = new JLabel("Winner");
		pnlBot.add(lblWinner);
		
		pnlTop = new JPanel();
		contentPane.add(pnlTop, BorderLayout.NORTH);
		
		pnlCommunity = new JPanel();
		pnlCommunity.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		contentPane.add(pnlCommunity, BorderLayout.WEST);
	
		pviews = new HashMap<Player, PlayerView>();
		
	}
	
	
	public void displayWinner(String msg)
	{
		lblWinner.setText(msg);
	}

	public void updateCommunity(Hand common) 
	{
		pnlCommunity.removeAll();
		
		CardView cv = null;
		for (Card c : common.getCards())
		{
			cv = new CardView(c);
			pnlCommunity.add(cv);
		}
	}

	
	public PlayerView getPlayerView(Player p)
	{
		return pviews.get(p);
	}
	
	public void addPlayerView(Player p)
	{
		PlayerView content = getPlayerView(p);
		
		if ( null == content)
		{
			content = new PlayerView(p);
			pviews.put(p, content);
			
			getPanelPlayers().add(content);
		}
	}
	
	
	public void updatePlayer(Player p) 
	{
		PlayerView content = getPlayerView(p);
		if ( null != content)
		{
			content.display(p.getHand());;
		}
	}
	
	

}
